package com.huayun.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;


import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ParsedResultType;
import com.huayun.base.BaseActivity;
import com.huayun.base.R;
import com.huayun.data.BaseConstant;
import com.huayun.util.PermissionUtils;
import com.huayun.util.utils.LogUtil;
import com.mylhyl.zxing.scanner.OnScannerCompletionListener;
import com.mylhyl.zxing.scanner.ScannerView;

/**
 * Description:
 * Created on 2019/8/14 0014 17:18:22
 * Organization:华云
 * author:AHuangSHang
 */
public class ScanActivity extends BaseActivity {
    public ScannerView scannerView;

    @Override
    protected void bindView() {
        scannerView.setOnScannerCompletionListener(new OnScannerCompletionListener() {
            @Override
            public void onScannerCompletion(Result rawResult, ParsedResult parsedResult, Bitmap barcode) {
                ParsedResultType type = parsedResult.getType();
                LogUtil.e("----->" + rawResult.getText());
                switch (type) {
                    case URI:
                        Intent intent = new Intent();
                        intent.putExtra(BaseConstant.SCAN_URL_DATA, rawResult.getText());
                        setResult(BaseConstant.RESULT_SCAN_URL_CODE, intent);
                        break;
                    case TEXT:
                        break;
                    default:
                        break;
                }
                ScanActivity.this.finish();
            }
        });
    }

    @Override
    protected void initView() {
        PermissionUtils.requestPermissionsWrapper(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, 0x9999);
        setTitle("扫描");
        scannerView = findViewById(R.id.scanner_view);
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected int getContentViewLayoutResId() {
        return R.layout.activity_scan;
    }

    @Override
    protected void onResume() {
        scannerView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        scannerView.onPause();
        super.onPause();
    }
}
